package nl.haemers.testrunners 
{
	import flash.events.Event;
	import nl.haemers.base.BaseSprite;
	import nl.haemers.data.serializers.XMLSerializer;
	import nl.haemers.test.data.vo.PageVO;
	import nl.haemers.test.data.vo.TestVO;
	import nl.haemers.test.data.vo.EnvironmentVO;
	import nl.haemers.timer.StopWatch;
	import nl.haemers.utils.Logger;
	import nl.haemers.data.serializers.ObjectSerializer;
	import flash.utils.*;
	/**
	 * @author Jonathan Haemers
	 */
	public class XMLRunner extends BaseSprite
	{
		public var xmlParser			:XMLSerializer;
		public var xml					:XML;
		public var vo					:TestVO;
		
		public var timer				:StopWatch;
		
		public function XMLRunner() 
		{
			
		}
		
		/** Public methods **/
		
		/** Getters & Setters **/
		
		/** Protected methods **/
		override protected function init(e:Event = null) : void
		{
			super.init(e);
			timer = new StopWatch();
			
			xml = getTestProjectXML();
			//vo = createVO();
			startup();
		}
		
		protected function startup() : void
		{
			Logger.i(describeType(new Vector.<EnvironmentVO>()));
			var vec:Vector.<EnvironmentVO> = Vector.<EnvironmentVO>([new EnvironmentVO()]);
			Logger.i(describeType(vec));
			xmlParser = new XMLSerializer();
			timer.start();
			var result:Array = xmlParser.deserialize(xml, EnvironmentVO);
			
			timer.stop();
			Logger.i(xml);
			for each(var test:EnvironmentVO in result)
			{
				
				Logger.i("Name: " + test.name);
				Logger.i("Host: " + test.host);
				Logger.i("Pages: " + test.pages + " - length: " + test.pages.length);
				Logger.i("Page: " + test.dataPage);
				if (test.dataPage)
				{
					Logger.i(test.dataPage.name);
					Logger.i(test.dataPage.url);
					if (test.dataPage.pageOwner)
					{
						Logger.i(test.dataPage.pageOwner.zipCode);
						Logger.i(test.dataPage.pageOwner.streetColor);
					}
				}
				
				Logger.i("Typed pages: " + test.typedPages);
				if (test.typedPages)
				{
					Logger.i("length: " + test.typedPages.length);
					for each(var page:PageVO in test.typedPages)
					{
						Logger.i("name: " + page.name);
						Logger.i("url: " + page.url);
					}
				}
				/*
				Logger.i("Title: " +test.title);
				Logger.i("ID: "+test.id.toString());
				Logger.i("xPos: "+test.xPos.toString());
				Logger.i("human: "+test.human.toString());
				
				Logger.i("street: "+test.street);
				Logger.i("color: "+test.color.toString());
				*/
			}
			//without complex data 79ms / 66ms
			Logger.i(timer.currentTime+"ms", timer);
		}
		
		protected function getTestProjectXML() : XML
		{
			var source:XML = <environments>
								<environment name="testserver">
									<host><![CDATA[http://testserver3.idreams.nl]]></host >
									<datapage>
										<name><![CDATA[xml page]]></name>
										<page>/xml_data.xml</page>
										<owner zipcode="4545BZ">
											<streetcolor>0xFF00FF</streetcolor>
										</owner>
									</datapage>
									<pages>
										<page name="video_data">
											<![CDATA[/test_data.json]]>
										</page>
										<page name="location_data">
											<![CDATA[/locations_data.json]]>
										</page>
									</pages>
									<typedpages>
										<page name="video_data">
											<![CDATA[/test_data.json]]>
										</page>
										<page name="location_data">
											<![CDATA[/locations_data.json]]>
										</page>
									</typedpages>
								</environment>
								<environment name="liveserver">
									<host><![CDATA[http://liveserver.idreams.nl]]></host>
									<pages>
										<page name="video_data">
											<![CDATA[/test_data.json]]>
										</page>
										<page name="location_data">
											<![CDATA[/locations_data.json]]>
										</page>
									</pages>
								</environment>
							</environments>
			
			return source;
		}
		
		protected function getXML() : XML
		{
			var source:XML = <employees>
								<employee index="123" ssn="11-55-66" human="true" position="10,115" color="0xFFFFFF">
									<name>
										<![CDATA[John Doe]]>
									</name>
									<address>
										<street>11 Main St.</street>
										<city>San Francisco</city>
										<state>CA</state>
										<zip>98765</zip>
									</address>
								</employee>
								<employee ssn="789-789-7890" human="false" position="10.25">
									<name>
										<![CDATA[Mary-ann Test]]>
									</name>
									<address>
										<street>99 Broad St.</street>
										<city>Newton</city>
										<state>MA</state>
										<zip>01234</zip>
									</address>
								</employee>
							</employees>;
			
			return source;
		}
		
		protected function createVO() : TestVO
		{
			return new TestVO();
		}
		/** Private methods **/
	}

}